/*
 * @lc app=leetcode.cn id=513 lang=cpp
 *
 * [513] 找树左下角的值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int findBottomLeftValue(TreeNode* root) {
        //利用dfs，在传统dfs上加上行号信息
        //最大行号即为答案
        dfs(root, 0);
        return ans;
    }

private:
    int ans;
    int maxlevel = INT_MIN;
    void dfs(TreeNode* t, int level){
        if(t == nullptr) return;
        if(level > maxlevel){
            ans = t->val;
            maxlevel = level;
        }
        dfs(t->left, level + 1);
        dfs(t->right, level + 1);
        return;
    }
};
// @lc code=end

